import 'dart:io';

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

//class JoyUrlLauncherService extends StatefulWidget{


class JoyUrlLauncherService {
  /// 启动Telegram链接
  static Future<bool> launchTelegramUrl(String telegramUsername) async {
    const telegramUrl = "https://t.me/TrendyHub2";
    const telegramScheme = "tg://resolve?domain=TrendyHub2";

    try {
      // 在Android上优先尝试浏览器模式
      if (Platform.isAndroid) {
        return await _launchWithFallback(telegramUrl, telegramScheme);
      } else {
        // iOS上先尝试应用，再尝试浏览器
        return await _launchWithAppFirst(telegramUrl, telegramScheme);
      }
    } catch (e) {
      debugPrint('Telegram launch error: $e');
      return false;
    }
  }

  /// Android优先浏览器的启动策略
  static Future<bool> _launchWithFallback(String webUrl, String appScheme) async {
    try {
      // 1. 首先尝试在浏览器中打开 (Android上更可靠)
      if (await canLaunchUrl(Uri.parse(webUrl))) {
        bool launched = await launchUrl(
          Uri.parse(webUrl),
          mode: LaunchMode.externalApplication,
          webViewConfiguration: const WebViewConfiguration(
            enableJavaScript: true,
            enableDomStorage: true,
          ),
        );
        if (launched) return true;
      }

      // 2. 尝试应用内浏览器
      bool inAppLaunched = await launchUrl(
        Uri.parse(webUrl),
        mode: LaunchMode.inAppBrowserView,
      );
      if (inAppLaunched) return true;

      // 3. 最后尝试Telegram应用
      if (await canLaunchUrl(Uri.parse(appScheme))) {
        return await launchUrl(
          Uri.parse(appScheme),
          mode: LaunchMode.externalApplication,
        );
      }

      return false;
    } catch (e) {
      debugPrint('Launch with fallback error: $e');
      return false;
    }
  }

  /// iOS优先应用的启动策略
  static Future<bool> _launchWithAppFirst(String webUrl, String appScheme) async {
    try {
      // 1. 首先尝试Telegram应用
      if (await canLaunchUrl(Uri.parse(appScheme))) {
        bool launched = await launchUrl(
          Uri.parse(appScheme),
          mode: LaunchMode.externalApplication,
        );
        if (launched) return true;
      }

      // 2. 尝试外部浏览器
      if (await canLaunchUrl(Uri.parse(webUrl))) {
        return await launchUrl(
          Uri.parse(webUrl),
          mode: LaunchMode.externalApplication,
        );
      }

      return false;
    } catch (e) {
      debugPrint('Launch with app first error: $e');
      return false;
    }
  }

  /// 通用URL启动方法
  static Future<bool> launchAnyUrl(String url) async {
    try {
      if (await canLaunchUrl(Uri.parse(url))) {
        return await launchUrl(
          Uri.parse(url),
          mode: Platform.isAndroid ? LaunchMode.externalApplication : LaunchMode.platformDefault,
        );
      }
      return false;
    } catch (e) {
      debugPrint('Launch any URL error: $e');
      return false;
    }
  }

  /// Discord启动方法
  static Future<bool> launchDiscordUrl(String discordUrl) async {
    try {
      if (await canLaunchUrl(Uri.parse(discordUrl))) {
        return await launchUrl(
          Uri.parse(discordUrl),
          mode: LaunchMode.externalApplication,
        );
      }
      return false;
    } catch (e) {
      debugPrint('Discord launch error: $e');
      return false;
    }
  }
}